package com.greendragon.service.backend.impl; 

import java.util.List;

import com.greendragon.persistence.dao.IBaseDao;
import com.greendragon.persistence.enmu.FetchOrderType;
import com.greendragon.persistence.enmu.LockType;
import com.greendragon.service.backend.IBaseBackendService;

/** 
 * @author wulibin, exceptsky@gmail.com, 2011-11-5 上午11:27:36
 * 
 */
public class BaseBackendService<T> implements IBaseBackendService<T> {
	protected IBaseDao<T> dao;
	protected int pageLength = 20;
	
	@Override
	public void add(T entity) {
		dao.add(entity);
	}

	@Override
	public void delete(long id) {
		dao.delete(id);
	}

	@Override
	public void deleteBatch(long[] ids) {
		dao.deleteBatch(ids);
	}

	@Override
	public T get(long id) {
		return dao.get(id);
	}

	@Override
	public void update(T entity) {
		dao.update(entity);
	}

	@Override
	public List<T> getAll(FetchOrderType orderType) {
		return dao.getAll(orderType);
	}

	
	@Override
	public List<T> getPage(int page, LockType lockType, FetchOrderType orderType) {
		return dao.getPage(page * pageLength, pageLength, lockType, orderType);
	}

	@Override
	public long getTotalCount(LockType lockType) {
		return dao.getTotalCount(lockType);
	}

	@Override
	public List<T> getByCondition(T expEntity, int page, FetchOrderType orderType) {
		return dao.getByCondition(expEntity, page*pageLength, pageLength, orderType);
	}
	
	@Override
	public List<T> getUnlock(int page){
		return dao.getPage(pageLength*page, pageLength, LockType.UNLOCK, FetchOrderType.CREATE_DESC);
	}

	@Override
	public List<T> getLock(int page){
		return dao.getPage(pageLength*page, pageLength, LockType.LOCK, FetchOrderType.CREATE_DESC);
	}

	@Override
	public long getCountByCondition(T expEntity) {
		return dao.getCountByCondition(expEntity);
	}
	
	@Override
	public void setPageLength(int length){
		this.pageLength = length;
	}
	
	@Override
	public int getPageLength(){
		return this.pageLength;
	}
	
	public void setDao(IBaseDao<T> dao) {
		this.dao = dao;
	}
}
 